package com.employee.service.impl;

import com.employee.exception.UserExistException;
import com.employee.mapper.StaffMapper;
import com.employee.mapper.UserMapper;
import com.employee.pojo.dto.*;
import com.employee.pojo.entry.Position;
import com.employee.pojo.entry.User;
import com.employee.pojo.vo.StaffVo;
import com.employee.service.StaffService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import java.util.UUID;

/**
 * @AUTHOR 尹书岿
 * @DATE 2025/1/9 18:48
 * @DESCRIBE
 */
@Service
public class StaffServiceImpl implements StaffService {

    @Autowired
    private StaffMapper staffMapper;

    @Autowired
    private UserMapper userMapper;

//    @Autowired
//    private

    @Override
    public List<StaffVo> getStaffList() {
        return  staffMapper.getStaffList();


    }

    @Override
    public void addStaff(StaffDTO staffDTO) {
        User user = userMapper.selectByEmail(new UserDTO(staffDTO.getEmail(), staffDTO.getPassword()));
        if (user == null) {
            staffDTO.setSalary(UUID.randomUUID().toString());
            staffMapper.addStaff(staffDTO);
        } else {
            throw new UserExistException("用户已存在！");
        }

    }

    @Override
    public void updateStaff(UpdateStaffDTO data) {
        SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        try {
            // 将字符串转换为日期类型
            Date birthDate = formatter.parse(data.getBirthStr());
            Date firstJobTime = formatter.parse(data.getFirstJobTimeStr());
            data.setBirth(birthDate);
            data.setFirstJobTime(firstJobTime);
            staffMapper.updateStaff(data);

        } catch (ParseException e) {
            e.printStackTrace();
        }

    }

    @Override
    public List<StaffVo> getStaffListByQuery(SelectStaffDTO selectStaffDTO) {

        String t = selectStaffDTO.getEntryTime().split("T")[0];
selectStaffDTO.setEntryTime(t);
        System.out.println(selectStaffDTO.getEntryTime());
        return staffMapper.getStaffListByQuery(selectStaffDTO);}

    @Override
    public void updateEmpStatus(Integer empId, String empStatus) {
        if(empStatus.equals("1")){
            staffMapper.updateEmpStatus(empId,"在职");
        }else{
            staffMapper.updateEmpStatus(empId,"离职");
        }
    }

    @Override
    public void updateEmpDeptId(UpdateEmpDeptDto updateEmpDeptDto) {
        staffMapper.updateEmpDeptId(updateEmpDeptDto);
    }

    @Override
    public void updateEmpPosId(UpdateEmpPosDto updateEmpPosDto) {
        staffMapper.updateEmpPosId(updateEmpPosDto);
    }


}
